package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

//    @PostMapping("/upload")
    public Result upload(String name, Integer age, MultipartFile image) throws Exception {

        String originalFilename = image.getOriginalFilename();

        //将文件存储在E:\image\images中（唯一且不能重复的）
        image.transferTo(new File("E:\\image\\images" + originalFilename));

        return Result.success();
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {

        String url = aliOSSUtils.upload(image);

        log.info("文件上传完成，文件访问的url:{}", url);

        return Result.success(url);
    }
}
